<?php
/* This file is part of a copyrighted work; it is distributed with NO WARRANTY.
 * See the file COPYRIGHT.html for more details.
 */
 
  require_once("../shared/common.php");
  $tab = "school";
  $restrictToMbrAuth = TRUE;
  $nav = "newconfirm_section";
  $restrictInDemo = true;
  require_once("../shared/logincheck.php");

  require_once("../classes/Section.php");
  require_once("../classes/SectionQuery.php");
  require_once("../classes/DmQuery.php");
  require_once("../functions/errorFuncs.php");
  require_once("../classes/Localize.php");
  $loc = new Localize(OBIB_LOCALE,$tab);

  #****************************************************************************
  #*  Checking for post vars.  Go back to form if none found.
  #****************************************************************************
  if (count($_POST) == 0) {
    header("Location: ../school/section_new_form.php");
    exit();
  }

  #****************************************************************************
  #*  Validate data
  #****************************************************************************
  $section = new Section();
  $section->setName($_POST["name"]);
  $_POST["name"] = $section->getName();
  $section->setSchoolId($_POST["schoolid"]);
  $_POST["schoolid"] = $section->getSchoolId();
  $section->setStudents($_POST["students"]);
  $_POST["students"] = $section->getStudents();
  $section->setTurnoId($_POST["turnoid"]);
  $_POST["turnoid"] = $section->getTurnoId();
  $section->setYear($_POST["year"]);
  $_POST["year"] = $section->getYear();
  if(isset($_POST["apply"])){
  	$section->setApply("S");
  	$_POST["apply"] = $section->getApply();
  }else{
  	$section->setApply("N");
  	$_POST["apply"] = $section->getApply();
  }
  $section->setClaveOne($_POST["claveone"]);
  $_POST["claveone"] = $section->getClaveOne();    
  
  $validData = $section->validateData();
  if (!$validData) {
    $pageErrors["year"] = $section->getYearError();
    $pageErrors["name"] = $section->getNameError();
    $pageErrors["claveone"] = $section->getClaveOneError();
    $_SESSION["postVars"] = $_POST;
    $_SESSION["pageErrors"] = $pageErrors;
    header("Location: ../school/section_new_form.php?schoolid=".$section->getSchoolId());
    exit();
  }

  #**************************************************************************
  #*  Check for duplicate clave one
  #**************************************************************************
  $sectionQ = new SectionQuery();
  $sectionQ->connect();
  if($section->getClaveOne() != "" && $section->getClaveOne() != 0){
  	$dupClaveOne= $sectionQ->DupClaveOne($section->getClaveOne());
  	if ($dupClaveOne) {
  		$pageErrors["claveone"] = $loc->getText("sectionDupClaveOne",array("claveone"=>$section->getClaveOne()));
  		$_SESSION["postVars"] = $_POST;
  		$_SESSION["pageErrors"] = $pageErrors;
  		header("Location: ../school/section_new_form.php?schoolid=".$section->getSchoolId());
  		exit();
  	}  	
  }
 

  #**************************************************************************
  #*  Insert new library member
  #**************************************************************************
  $sectionid = $sectionQ->insert($section);
  $sectionQ->close();

  #**************************************************************************
  #*  Destroy form values and errors
  #**************************************************************************
  unset($_SESSION["postVars"]);
  unset($_SESSION["pageErrors"]);

  $msg = $loc->getText("sectionNewSuccess");
  header("Location: ../school/school_view.php?schoolid=".U($section->getSchoolId())."&reset=Y&msg=".U($msg));
  exit();
?>
